package org.jimm.app;

import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * User: LSDsl
 * Date: 01.04.11
 * Time: 13:03
 */
public class NotificationManager {

    //TODO: need to add maps of accounts to support multiaccounts
    private static HashMap<String, Integer> cont2notifId = new HashMap<String, Integer>();
    private static int ids = 0;
    private static android.app.NotificationManager mNotificationManager;

    private static ArrayList<String> skipList = new ArrayList<String>();

    public static void addNotification(Context context, String contactName, String accountId, String msgText) {

        if (skipList.indexOf(contactName) == -1) {
            Intent intent = new Intent(context, ChatActivity.class);
            intent.putExtra(ChatActivity.CONTACT_ID_EXTRA, contactName);
            intent.putExtra(ChatActivity.ACCOUNT_ID_EXTRA, accountId);

            mNotificationManager = (android.app.NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            int icon = android.R.drawable.sym_action_chat;
            CharSequence tickerText = contactName + ":" + msgText;
            long when = System.currentTimeMillis();
            Notification notification = new Notification(icon, tickerText, when);
            CharSequence contentTitle = contactName;
            CharSequence contentText = msgText;
            PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0); // РџРѕРґСЂРѕР±РЅРѕРµ РѕРїРёСЃР°РЅРёРµ РІ UPD Рє СЃС‚Р°С‚СЊРµ
            notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); // РџРµСЂРµРґР°РµРј РІ РЅР°С€Рµ СѓРІРµРґРѕРјР»РµРЅРёРµ РїР°СЂР°РјРµС‚СЂС‹ РІРёРґР° РїСЂРё СЂР°Р·РІРµСЂРЅСѓС‚РѕР№ СЃС‚СЂРѕРєРµ СЃРѕСЃС‚РѕСЏРЅРёСЏ
            Integer id = cont2notifId.get(contactName);
            if (id == null) {
                id = ids++;
                cont2notifId.put(contactName, id);
            }
            mNotificationManager.notify(id, notification); // Р� РЅР°РєРѕРЅРµС† РїРѕРєР°Р·С‹РІР°РµРј РЅР°С€Рµ СѓРІРµРґРѕРјР»РµРЅРёРµ С‡РµСЂРµР· РјРµРЅРµРґР¶РµСЂ РїРµСЂРµРґР°РІ РµРіРѕ ID
        }
    }

    public static void clearNotification(String contactName) {
        final Integer id = cont2notifId.get(contactName);
        if (id != null) mNotificationManager.cancel(id);
    }

    public static void skipNotifyFrom(String contactName) {
        skipList.add(contactName);
    }

    public static void revertNotifyFrom(String contactName) {
        skipList.remove(contactName);
    }
}
